//
//  MilitaryViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MilitaryViewController.h"
#import "PhoneixNews.pch"
@interface MilitaryViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mutableArray;
@property (nonatomic, retain) NSMutableArray *LBmutableArray;
@end

@implementation MilitaryViewController
- (void)dealloc {
    [_tableView release];
    [_LBmutableArray release];
    [_mutableArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 130) style: UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[HeadlineTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[HeadlineThreeImageTableViewCell class] forCellReuseIdentifier:@"cellThreeImage"];
    [self.view addSubview:_tableView];
    [_tableView release];
    [self getData];
    
}
#pragma mark - 数据解析
- (void)getData {
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:Military parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject;
        NSDictionary *dic = [array firstObject];
        NSArray *arrayTwo = dic[@"item"];
        self.mutableArray = [HeadlineModel modelHanderWithArray:arrayTwo];
        
        // 轮播图数据
        NSDictionary *LBdic = [array lastObject];
        NSArray *LBarray = LBdic[@"item"];
        self.LBmutableArray = [HeadlineModel modelHanderWithArray:LBarray];
        
        [_tableView reloadData];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

#pragma mark - TableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mutableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_mutableArray.count != 0) {
        HeadlineModel *headline = _mutableArray[indexPath.row];
        if (headline.images == nil) {
            HeadlineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            
            
            [cell.TitleImage sd_setImageWithURL:[NSURL URLWithString:headline.thumbnail]];
            cell.title.text = headline.title;
            cell.comments.text = headline.commentsall;
            cell.Commentsimage.image = [UIImage imageNamed:@"imageComments"];
            cell.time.text = [headline.updateTime substringWithRange:NSMakeRange(10, 6)];
            return cell;
        }else {
            HeadlineThreeImageTableViewCell *cellThreeImage = [tableView dequeueReusableCellWithIdentifier:@"cellThreeImage"];
            
            [cellThreeImage.Threeimage sd_setImageWithURL:[NSURL URLWithString:headline.images[0]]];
            [cellThreeImage.ThreeimageTwo sd_setImageWithURL:[NSURL URLWithString:headline.images[1]]];
            [cellThreeImage.ThreeimageThree sd_setImageWithURL:[NSURL URLWithString:headline.images[2]]];
            cellThreeImage.title.text = headline.title;
            cellThreeImage.time.text = [headline.updateTime substringWithRange:NSMakeRange(10, 6)];
            cellThreeImage.comments.text = headline.commentsall;
            cellThreeImage.Commentsimage.image = [UIImage imageNamed:@"imageComments"];
            return cellThreeImage;
            
        }

    }
    return nil;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeadlineModel *headline = _mutableArray[indexPath.row];
    if (headline.images == nil) {
        return 130;
    }else {
        return 240;
    }
    
}
#pragma mark - Cell点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HeadlineModel *headline = _mutableArray[indexPath.row];
    DetailePageViewController *detaile = [[DetailePageViewController alloc] init];
    detaile.urlStr = headline.commentsUrl;
    [self.navigationController pushViewController:detaile animated:YES];
}
#pragma mark -头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200;
}

#pragma mark - 头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *Arraytitle = [NSMutableArray array];
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    headView.backgroundColor =[UIColor redColor];
    [self.view addSubview:headView];
    for (int i = 0; i < _LBmutableArray.count; i++) {
        HeadlineModel *headline = _LBmutableArray[i];
        // 轮播图图片数组
        [array addObject:headline.thumbnail];
        // 轮播图标题数组
        [Arraytitle addObject:headline.title];
    }
    
    
    return headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
